<?php
header('Content-Type: application/json; charset=utf-8');

// 引入数据库配置
$dbConfig = include('db_config.php');

// 获取前端传过来的视频信息
$user_id = $_POST['user_id'] ?? '';
$video_title = $_POST['video_title'] ?? '';
$video_description = $_POST['video_description'] ?? '';
$video_duration = $_POST['video_duration'] ?? '';
$video_format = $_POST['video_format'] ?? '';
$video_likes = $_POST['video_likes'] ?? '';
$video_link = $_POST['video_link'] ?? '';
$video_cover = $_POST['video_cover'] ?? '';
$video_remarks = $_POST['video_remarks'] ?? '';
$review_status = $_POST['review_status'] ?? '假';
$reviewer_id = $_POST['reviewer_id'] ?? '';
$rejection_reason = $_POST['rejection_reason'] ?? '';

// 检查用户 ID 和视频标题是否为空
if (empty($user_id) || empty($video_title)) {
    $result = [
        'status' => 'error',
        'message' => '用户 ID 和视频标题不能为空'
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

// 检查视频链接和封面链接是否为空
if (empty($video_link) || empty($video_cover)) {
    $result = [
        'status' => 'error',
        'message' => '视频链接和封面链接不能为空'
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

// 创建数据库连接
try {
    $dsn = "mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbname'] . ";charset=" . $dbConfig['charset'];
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 插入视频数据到 mi_videos 表
    $insertVideoStmt = $pdo->prepare("INSERT INTO mi_videos (
                上传者用户的id, 视频标题, 视频介绍, 视频时长, 视频格式, 
                获赞数量, 视频链接, 视频封面, 视频备注, 是否已审核, 审核者的用户id, 拒绝原因
            ) VALUES (
                :user_id, :video_title, :video_description, :video_duration, :video_format, 
                :video_likes, :video_link, :video_cover, :video_remarks, :review_status, :reviewer_id, :rejection_reason
            )");

    // 绑定参数
    $insertVideoStmt->bindParam(':user_id', $user_id);
    $insertVideoStmt->bindParam(':video_title', $video_title);
    $insertVideoStmt->bindParam(':video_description', $video_description);
    $insertVideoStmt->bindParam(':video_duration', $video_duration);
    $insertVideoStmt->bindParam(':video_format', $video_format);
    $insertVideoStmt->bindParam(':video_likes', $video_likes);
    $insertVideoStmt->bindParam(':video_link', $video_link);
    $insertVideoStmt->bindParam(':video_cover', $video_cover);
    $insertVideoStmt->bindParam(':video_remarks', $video_remarks);
    $insertVideoStmt->bindParam(':review_status', $review_status);
    $insertVideoStmt->bindParam(':reviewer_id', $reviewer_id);
    $insertVideoStmt->bindParam(':rejection_reason', $rejection_reason);

    // 执行插入操作
    if ($insertVideoStmt->execute()) {
        // 插入成功
        $result = [
            'status' => 'success',
            'message' => '视频信息添加成功',
            'video_id' => $pdo->lastInsertId()
        ];
    } else {
        $result = [
            'status' => 'error',
            'message' => '视频信息添加失败'
        ];
    }

    echo json_encode($result, JSON_UNESCAPED_UNICODE);

} catch (PDOException $e) {
    // 数据库操作失败
    $result = [
        'status' => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
}
?>
